<?php
$basedir = dirname(__FILE__);

require_once $basedir . '/../classes/minime.php';
require_once $basedir . '/../extlib/Crypt/RSA.php';
require_once $basedir . '/../extlib/Crypt/Random.php';
require_once $basedir . '/../extlib/Crypt/Hash.php';
require_once $basedir . '/../extlib/Math/BigInteger.php';

MiniMe::initialize();

if( count($argv) != 5) {
      echo "\n\tERROR!";
      echo "\n\t\tPlease provide USERNAME NAME TITLE SUBTITLE as parameters to this function.";
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php createuser.php koski "Tuomas Koski" "Tuomas Koski\'s microblog feed." "My Random Thoughts."';
      echo "\n\n";
      exit;
}

$username = $argv[1];
$name     = $argv[2];
$title    = $argv[3];
$subtitle = $argv[4];

echo "\n\tCreating new user:";


echo "\n\t\t- Creating public and private keys ... (this will take awhile) ...";
$keyGenerator = new Magic_Signature_KeyGenerator();
$keyGenerator->generatePublicKeyPrivateKey();
$privateKey = $keyGenerator->privateKeyAsString();
$publicKey = $keyGenerator->publicKeyAsString();
echo "OK!";


echo "\n\t\t- Creating user profile (user.xml) ... ";
$arrayUser['uri'] = Minime_HrefRenderer::getLdrrProfileHref($username, MiniMe::$conf);
$arrayUser['name'] = $name;
$arrayUser['jid'] = $username . '@' . MiniMe::$conf['env']['domain'];
$arrayUser['privatekey'] = $privateKey;
$arrayUser['publickey'] = $publicKey;
$arrayUser['title'] = $title;
$arrayUser['subtitle'] = $subtitle;
$arrayUser['logo'] = Minime::$conf['default']['logo'];

$user = Minime_User::buildFromArray($arrayUser);
echo "OK!";


echo "\n\t\t- Creating user subscriptions (subscriptions.xml) ... ";
$subs = new Minime_User_Subscriptions();
echo "OK!";


echo "\n\t\t- Creating user subscribers (subscribers.xml) ... ";
$subscr = new Minime_User_Subscribers();
echo "OK!";


echo "\n\t\t- Creating initial feed (atom.xml) ... ";
// Let's create the needed elements.
$generator = new Atom_Feed_Generator("http://madebymonsieur.com", "MiniMe - Life to XML");
$id = new Atom_Feed_Id($user->getUri());
$title = new Atom_Feed_Title($user->getTitle());
$subtitle = new Atom_Feed_Subtitle($user->getSubtitle());
$logo = new Atom_Feed_Logo($user->getLogo());
$updated = new Atom_Feed_Updated();

$author = new Atom_Feed_Author();
$author->addChildElement(new Atom_Feed_Author_Name($user->getName()));
$author->addChildElement(new Atom_Feed_Author_Uri($user->getUri()));


// Let's create the activity actor
$as_subject = new Activitystream_Subject();
$as_subject->addChildElement(new Activitystream_ObjectType(Activitystream_ObjectType::TYPE_PERSON));

$as_subject->addChildElement(new Atom_Feed_Id($user->getUri()));
$as_subject->addChildElement(new Atom_Feed_Title($user->getName()));
$as_subject->addChildElement(new Atom_Feed_Link(Atom_Feed_Link::REL_ALTERNATE, 
                                               Atom_Feed_Link::TYPE_TEXTHTML, 
                                               $user->getUri()));
$as_subject->addChildElement(new Atom_Feed_Link_Avatar($user->getLogo(), "image/jpeg"));
// Finally let's create the Atom Entry itself
$atom = new Atom_Feed();

$atom->addChildElement($generator);
$atom->addChildElement($id);
$atom->addChildElement($title);
$atom->addChildElement($subtitle);
$atom->addChildElement($logo);
$atom->addChildElement($updated);
$atom->addChildElement($author);

$atom->addChildElement(new Atom_Feed_Link_AlternateHtml($user->getUri()));
$atom->addChildElement(new Atom_Feed_Link_Hub(Minime_HrefRenderer::getLdrrHubHref($username, MiniMe::$conf)));
$atom->addChildElement(new Atom_Feed_Link_Salmon(Minime_HrefRenderer::getLdrrSalmonHref($username, MiniMe::$conf)));
$atom->addChildElement(new Atom_Feed_Link_Self(Minime_HrefRenderer::getLdrrAtomHref($username, MiniMe::$conf)));

$atom->addChildElement($as_subject);

// Let's add needed new namespaces.
$atom->addNamespace(ActivityStream::AS_NS_PREFIX, Activitystream::AS_NS);
$atom->addNamespace(Mm_Atom_Element_Poco::NS_PREFIX, Mm_Atom_Element_Poco::NS);
echo "OK!";

//echo $atom->asXML();


echo "\n\t\t- Writing user profile (user.xml) ... ";
$userStorer = new Minime_User_Storer(MiniMe::$conf);
$userStorer->stroreUser($username, $user);
echo "OK!";


echo "\n\t\t- Writing subscriptions (subscriptions.xml) ... ";
$subsStorer = new Minime_User_Subscriptions_Storer(MiniMe::$conf);
$subsStorer->storeSubscription($username, $subs);
echo "OK!";


echo "\n\t\t- Writing subscribers (subscribers.xml) ... ";
$subsStorer = new Minime_User_Subscribers_Storer(MiniMe::$conf);
$subsStorer->storeSubscribers($username, $subscr);
echo "OK!";


echo "\n\t\t- Writing initial atom feed (atom.xml) ... ";
$atomStorer = new Minime_Atom_Storer(MiniMe::$conf);
$atomStorer->storeAtom($username, $atom);
echo "OK!";



echo "\n\n\tDONE!\n";

